#!usr/bin/python
#coding=utf-8



import sys
import re
import time
import urllib.request
import chardet
import ltmex.ltmcode
import ltmex.ltmgrab
import ltmex.ltmsearch
import ltmex.ltmFile

import urllib.parse
import string

name = 1

def saveFile(data,path):
    f = open(path, 'w')
    f.write(data)
    f.close()



def getImgByUrl(url):
    global name
    
    imglist = ltmex.ltmgrab.grabHtml(pattern =ltmex.ltmgrab.patHttpVideo,url= url, headers=ltmex.ltmgrab.defaultHeader)
#    print(imglist)
    for img in imglist:
        print(img)
#        saveFile(img, "c:/t/%s.txt" %(name))
#        ltmex.ltmFile.writeFile(img, "c:/t/%.mp4" %(name))
        ltmex.ltmFile.downloadFile(img[0], 'c:/t/%s.%s' % (name,img[1]))
#       urllib.request.urlretrieve(img[0], 'c:/t/%s.%s' % (name,img[1])  )
        name += 1

'''
u = 'http://www.baidu.com/s?wd=大&rsv_bp=0&rsv_spt=3&rsv_n=2&inputT=6391'
u =urllib.parse.quote(u,safe=string.printable)
print(u)
linklist = ltmex.ltmgrab.grabHtml('href="(https*://www.baidu.com/link\?[^"]+?)"', u)
'''

#linklist = ltmex.ltmgrab.grabHtml('href="(https*://www.baidu.com/link\?[^"]+?)"', "http://www.baidu.com/s?wd=谷%20磁&rsv_bp=0&rsv_spt=3&rsv_n=2&inputT=6391")


linklist = ltmex.ltmsearch.searchBaidu(['小视频'], num=200)

print(linklist)
for link in linklist:

    getImgByUrl(link)



#getImgByUrl('http://www.baidu.com/link?url=UKC-gQlc6ULHiYQAOHzb58ltnoKHrSufyqpxELPE6a1I6kilQLDjUPhphLhG05W4d7NYJnRmhWIVZ_fvBXT6-SxZrA8OVMR6TtMhTTmJxsGL0FT6R6DNoyo-N6Ll5UNk')


